package gui.standard.form;

import java.math.BigDecimal;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.tablemodel.MagacinskaKarticaTableModel;
import net.miginfocom.swing.MigLayout;
import util.components.JDecimalTextField;
import util.components.JDigitsTextField;
import util.components.JIznosTextField;
import util.components.JLimitedTextField;
import util.exceptions.NegativeValueException;
import actions.standard.form.ZoomFormAction;

public class MagacinskaKarticaForm extends StandardForm {

	private static final long serialVersionUID = 1L;
	private JLimitedTextField tfSifraPreduzeca;
	private JLimitedTextField tfSifraSektora;
	private JLimitedTextField tfSifraMagacina;
	private JLimitedTextField tfSifraGrupe;
	private JDigitsTextField tfSifraArtikla;
	private JDigitsTextField tfPoslovnaGodina;
	private JIznosTextField tfProsecnaCena;
	private JIznosTextField tfZadnjaNabavnaCena;
	private JIznosTextField tfPlanskaCena;
	private JDecimalTextField tfKolPocenta;
	private JDecimalTextField tfKolUlaza;
	private JDecimalTextField tfKolIzlaza;
	private JDecimalTextField tfKolUkupna;
	private JIznosTextField tfVrPocetna;
	private JIznosTextField tfVrUlaza;
	private JIznosTextField tfVrIzlaza;
	private JIznosTextField tfVrUkupna;

	private JButton bMagacinZoom;
	private JButton bArtikalZoom;
	private JButton bPoslovnaGodinaZoom;

	public MagacinskaKarticaForm() {

	}
	
	@Override
	public void init(){
		
		super.init();
		
		tfSifraPreduzeca = new JLimitedTextField(12, 12, true);
		tfSifraSektora = new JLimitedTextField(12, 12, true);
		tfSifraMagacina = new JLimitedTextField(12, 12, true);
		tfSifraGrupe = new JLimitedTextField(12, 12, true);
		tfSifraArtikla = new JDigitsTextField(12, 12, true, true);
		tfPoslovnaGodina = new JDigitsTextField(4, 4, false, false);
		tfProsecnaCena = new JIznosTextField(12, 12, 2, false);
		tfZadnjaNabavnaCena = new JIznosTextField(12, 12, 2, false);
		tfPlanskaCena = new JIznosTextField(12, 12, 2, false);
		tfKolPocenta = new JDecimalTextField(13, 12, 2, ',', true);
		tfKolUlaza = new JDecimalTextField(13, 12, 2, ',', true);
		tfKolIzlaza = new JDecimalTextField(13, 12, 2, ',', true);
		tfKolUkupna = new JDecimalTextField(13, 12, 2, ',', true);
		tfVrPocetna = new JIznosTextField(12, 12, 2, false);
		tfVrUlaza = new JIznosTextField(12, 12, 2, false);
		tfVrIzlaza = new JIznosTextField(12, 12, 2, false);
		tfVrUkupna = new JIznosTextField(12, 12, 2, false);
		
		setTitle("Magacinska kartica");
		
		tableModel = new MagacinskaKarticaTableModel(
				new String[] { "Sifra preduzeca", "Sifra sektora",
						"Sifra magacina", "Sifra grupe", "Sifra artikala",
						"Poslovna godina", "Prosecna cena", "Zadnja nabavna cena",
						"Planska cena", "Kol. pocetna", "Kol. ulaza",
						"Kol. izlaza", "Kol. ukupna", "Vr. pocetna", "Vr. ulaza",
						"Vr. izlaza", "Vr. ukupna" }, 0);
		tblGrid.setModel(tableModel);
		
		bMagacinZoom = new JButton(new ZoomFormAction(this,
				new MagacinForm()));
		bArtikalZoom = new JButton(new ZoomFormAction(this,
				new ArtikalForm()));
		bPoslovnaGodinaZoom = new JButton(new ZoomFormAction(this,
				new PoslovnaGodinaForm()));
		
		children.add(new AnalitikaMagacinskeKarticeForm());
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new MigLayout("fillx"));
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new MigLayout("gapx 15px"));

		JLabel lblSifraPreduzeca = new JLabel("Sifra preduzeca:");
		JLabel lblSifraSektora = new JLabel("Sifra sektora:");
		JLabel lblSifraMagacina = new JLabel("Sifra magacina:");
		JLabel lblSifraGrupe = new JLabel("Sifra grupe:");
		JLabel lblSifraArtikla = new JLabel("Sifra artikla:");
		JLabel lblPoslovnaGodina = new JLabel("Poslovna godina:");
		JLabel lblProsecnaCena = new JLabel("Prosecna cena:");
		JLabel lblZadnjaNabavnaCena = new JLabel("Zadnja nabavna cena:");
		JLabel lblPlanskaCena = new JLabel("Planska cena:");
		JLabel lblKolPocetna = new JLabel("Kol. pocetna:");
		JLabel lblKolUlaza = new JLabel("Kol. ulaza:");
		JLabel lblKolIzlaza = new JLabel("Kol. izlaza:");
		JLabel lblKolUkupna = new JLabel("Kol. ukupna:");
		JLabel lblVrPocetna = new JLabel("Vr. pocetna:");
		JLabel lblVrUlaza = new JLabel("Vr. ulaza:");
		JLabel lblVrIzlaza = new JLabel("Vr. izala:");
		JLabel lblVrUkupna = new JLabel("Vr. ukupna:");

		dataPanel.add(lblSifraPreduzeca);
		dataPanel.add(tfSifraPreduzeca);
		dataPanel.add(bPoslovnaGodinaZoom, "wrap");
		dataPanel.add(lblSifraSektora);
		dataPanel.add(tfSifraSektora, "pushx");
		dataPanel.add(lblSifraMagacina);
		dataPanel.add(tfSifraMagacina);
		dataPanel.add(bMagacinZoom, "wrap");
		dataPanel.add(lblSifraGrupe);
		dataPanel.add(tfSifraGrupe, "pushx");
		dataPanel.add(lblSifraArtikla);
		dataPanel.add(tfSifraArtikla);
		dataPanel.add(bArtikalZoom, "wrap");
		dataPanel.add(lblPoslovnaGodina);
		dataPanel.add(tfPoslovnaGodina, "pushx");
		dataPanel.add(lblProsecnaCena);
		dataPanel.add(tfProsecnaCena, "wrap");
		dataPanel.add(lblZadnjaNabavnaCena);
		dataPanel.add(tfZadnjaNabavnaCena, "pushx");
		dataPanel.add(lblPlanskaCena);
		dataPanel.add(tfPlanskaCena, "wrap");
		dataPanel.add(lblKolPocetna);
		dataPanel.add(tfKolPocenta, "pushx");
		dataPanel.add(lblKolUlaza);
		dataPanel.add(tfKolUlaza, "wrap");
		dataPanel.add(lblKolIzlaza);
		dataPanel.add(tfKolIzlaza, "pushx");
		dataPanel.add(lblKolUkupna);
		dataPanel.add(tfKolUkupna, "wrap");
		dataPanel.add(lblVrPocetna);
		dataPanel.add(tfVrPocetna, "pushx");
		dataPanel.add(lblVrUlaza);
		dataPanel.add(tfVrUlaza, "wrap");
		dataPanel.add(lblVrIzlaza);
		dataPanel.add(tfVrIzlaza, "pushx");
		dataPanel.add(lblVrUkupna);
		dataPanel.add(tfVrUkupna, "wrap");
		bottomPanel.add(dataPanel);

		bottomPanel.add(buttonsPanel, "dock east");

		add(bottomPanel, "grow, wrap");
	}

	@Override
	public void sync() {
		// TODO Auto-generated method stub
		int index = tblGrid.getSelectedRow();
		if (index < 0) {
			tfSifraPreduzeca.setText("");
			tfSifraSektora.setText("");
			tfSifraMagacina.setText("");
			tfSifraGrupe.setText("");
			tfSifraArtikla.setText("");
			tfPoslovnaGodina.setText("");
			tfProsecnaCena.setText("");
			tfZadnjaNabavnaCena.setText("");
			tfPlanskaCena.setText("");
			tfKolPocenta.setText("");
			tfKolUlaza.setText("");
			tfKolIzlaza.setText("");
			tfKolUkupna.setText("");
			tfVrPocetna.setText("");
			tfVrUlaza.setText("");
			tfVrIzlaza.setText("");
			tfVrUkupna.setText("");
			return;
		}
		String sifraPreduzeca = (String) tableModel.getValueAt(index, 0);
		String sifraSektora = (String) tableModel.getValueAt(index, 1);
		String sifraMagacina = (String) tableModel.getValueAt(index, 2);
		String sifraGrupe = (String) tableModel.getValueAt(index, 3);
		String sifraArtikla = (String) tableModel.getValueAt(index, 4);
		String poslovnaGodina = (String) tableModel.getValueAt(index, 5);
		String prosecnaCena = (String) tableModel.getValueAt(index, 6);
		String zadnjaNabavnaCena = (String) tableModel.getValueAt(index, 7);
		String planskaCena = (String) tableModel.getValueAt(index, 8);
		String kolPocetna = (String) tableModel.getValueAt(index, 9);
		String kolUlaza = (String) tableModel.getValueAt(index, 10);
		String kolIzlaza = (String) tableModel.getValueAt(index, 11);
		String kolUkupna = (String) tableModel.getValueAt(index, 12);
		String vrPocetna = (String) tableModel.getValueAt(index, 13);
		String vrUlaza = (String) tableModel.getValueAt(index, 14);
		String vrIzlaza = (String) tableModel.getValueAt(index, 15);
		String vrUkupna = (String) tableModel.getValueAt(index, 16);
		
		BigDecimal bdProsecnaCena = new BigDecimal(prosecnaCena);
		BigDecimal bdZadnjaNabavnaCena = new BigDecimal(zadnjaNabavnaCena);
		BigDecimal bdPlanskaCena = new BigDecimal(planskaCena);
		BigDecimal bdVrUlaza = new BigDecimal(vrUlaza);
		BigDecimal bdVrIzlaza = new BigDecimal(vrIzlaza);
		BigDecimal bdVrUkupna = new BigDecimal(vrUkupna);
		
		
		kolPocetna = kolPocetna.replace(",", ".");
		kolUlaza = kolUlaza.replace(",", ".");
		kolIzlaza = kolIzlaza.replace(",", ".");
		kolUkupna = kolUkupna.replace(",", ".");
		
		double dkolPocetna = Double.parseDouble(kolPocetna);
		double dkolUlaza = Double.parseDouble(kolUlaza);
		double dkolIzlaza = Double.parseDouble(kolIzlaza);
		double dkolUkupna = Double.parseDouble(kolUkupna);

		tfSifraPreduzeca.setText(sifraPreduzeca);
		tfSifraSektora.setText(sifraSektora);
		tfSifraMagacina.setText(sifraMagacina);
		tfSifraGrupe.setText(sifraGrupe);
		tfSifraArtikla.setText(sifraArtikla);
		tfPoslovnaGodina.setText(poslovnaGodina);
		try {
			tfProsecnaCena.setValue(bdProsecnaCena);
		} catch (NegativeValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			tfZadnjaNabavnaCena.setValue(bdZadnjaNabavnaCena);
		} catch (NegativeValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			tfPlanskaCena.setValue(bdPlanskaCena);
		} catch (NegativeValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tfKolPocenta.setValue(dkolPocetna);
		tfKolUlaza.setValue(dkolUlaza);
		tfKolIzlaza.setValue(dkolIzlaza);
		tfKolUkupna.setValue(dkolUkupna);
		tfVrPocetna.setText(vrPocetna);
		try {
			tfVrUlaza.setValue(bdVrUlaza);
		} catch (NegativeValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			tfVrIzlaza.setValue(bdVrIzlaza);
		} catch (NegativeValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			tfVrUkupna.setValue(bdVrUkupna);
		} catch (NegativeValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setMode(StandardForm.MODE_EDIT);

	}

	@Override
	public void addRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String sifraMagacina = tfSifraMagacina.getText().trim();
		String sifraGrupe = tfSifraGrupe.getText().trim();
		String sifraArtikla = tfSifraArtikla.getText().trim();
		String poslovnaGodina = tfPoslovnaGodina.getText().trim();
		String prosecnaCena = tfProsecnaCena.getText().trim();
		String zadnjaNabavnaCena = tfZadnjaNabavnaCena.getText().trim();
		String planskaCena = tfPlanskaCena.getText().trim();
		String kolPocetna = Double.toString(tfKolPocenta.getValue());
		String kolUlaza = Double.toString(tfKolUlaza.getValue());
		String kolIzlaza = Double.toString(tfKolIzlaza.getValue());
		String kolUkupna = Double.toString(tfKolUkupna.getValue());
		String vrPocetna = tfVrPocetna.getValue().toString();
		String vrUlaza = tfVrUlaza.getValue().toString();
		String vrIzlaza = tfVrIzlaza.getValue().toString();
		String vrUkupna = tfVrUkupna.getValue().toString();
		try {
			int index = tableModel.insertRow(new String[] { sifraPreduzeca,
					sifraSektora, sifraMagacina, sifraGrupe, sifraArtikla,
					poslovnaGodina, prosecnaCena, zadnjaNabavnaCena,
					planskaCena, kolPocetna, kolUlaza, kolIzlaza, kolUkupna,
					vrPocetna, vrUlaza, vrIzlaza, vrUkupna });
			tblGrid.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void searchRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String sifraMagacina = tfSifraMagacina.getText().trim();
		String sifraGrupe = tfSifraGrupe.getText().trim();
		String sifraArtikla = tfSifraArtikla.getText().trim();
		String poslovnaGodina = tfPoslovnaGodina.getText().trim();
		String prosecnaCena = tfProsecnaCena.getText().trim();
		String zadnjaNabavnaCena = tfZadnjaNabavnaCena.getText().trim();
		String planskaCena = tfPlanskaCena.getText().trim();
		String kolPocetna = Double.toString(tfKolPocenta.getValue());
		String kolUlaza = Double.toString(tfKolUlaza.getValue());
		String kolIzlaza = Double.toString(tfKolIzlaza.getValue());
		String kolUkupna = Double.toString(tfKolUkupna.getValue());
		String vrPocetna = tfVrPocetna.getValue().toString();
		String vrUlaza = tfVrUlaza.getValue().toString();
		String vrIzlaza = tfVrIzlaza.getValue().toString();
		String vrUkupna = tfVrUkupna.getValue().toString();
		try {
			tableModel.searchRows(new String[] { sifraPreduzeca, sifraSektora,
					sifraMagacina, sifraGrupe, sifraArtikla, poslovnaGodina,
					prosecnaCena, zadnjaNabavnaCena, planskaCena, kolPocetna,
					kolUlaza, kolIzlaza, kolUkupna, vrPocetna, vrUlaza,
					vrIzlaza, vrUkupna });
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void editRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String sifraMagacina = tfSifraMagacina.getText().trim();
		String sifraGrupe = tfSifraGrupe.getText().trim();
		String sifraArtikla = tfSifraArtikla.getText().trim();
		String poslovnaGodina = tfPoslovnaGodina.getText().trim();
		String prosecnaCena = tfProsecnaCena.getText().trim();
		String zadnjaNabavnaCena = tfZadnjaNabavnaCena.getText().trim();
		String planskaCena = tfPlanskaCena.getText().trim();
		String kolPocetna = Double.toString(tfKolPocenta.getValue());
		String kolUlaza = Double.toString(tfKolUlaza.getValue());
		String kolIzlaza = Double.toString(tfKolIzlaza.getValue());
		String kolUkupna = Double.toString(tfKolUkupna.getValue());
		String vrPocetna = tfVrPocetna.getValue().toString();
		String vrUlaza = tfVrUlaza.getValue().toString();
		String vrIzlaza = tfVrIzlaza.getValue().toString();
		String vrUkupna = tfVrUkupna.getValue().toString();
		try {
			int index = tableModel.editRow(new String[] { sifraPreduzeca,
					sifraSektora, sifraMagacina, sifraGrupe, sifraArtikla,
					poslovnaGodina, prosecnaCena, zadnjaNabavnaCena,
					planskaCena, kolPocetna, kolUlaza, kolIzlaza, kolUkupna,
					vrPocetna, vrUlaza, vrIzlaza, vrUkupna },
					tblGrid.getSelectedRow());
			tblGrid.setRowSelectionInterval(index, index);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void setMode(int mode) {
		super.setMode(mode);

		if (mode != StandardForm.MODE_ADD)
			return;

		tfSifraPreduzeca.setText("");
		tfSifraSektora.setText("");
		tfSifraMagacina.setText("");
		tfSifraGrupe.setText("");
		tfSifraArtikla.setText("");
		tfPoslovnaGodina.setText("");
		tfProsecnaCena.setText("");
		tfZadnjaNabavnaCena.setText("");
		tfPlanskaCena.setText("");
		tfKolPocenta.setText("");
		tfKolUlaza.setText("");
		tfKolIzlaza.setText("");
		tfKolUkupna.setText("");
		tfVrPocetna.setText("");
		tfVrUlaza.setText("");
		tfVrIzlaza.setText("");
		tfVrUkupna.setText("");
	}

	@Override
	public String[] getSelectedRowKeys() {
		// TODO Auto-generated method stub
		return new String[]{tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim(), tfSifraMagacina.getText().trim(), 
				tfSifraGrupe.getText().trim(), tfSifraArtikla.getText().trim(), tfPoslovnaGodina.getText().trim()};
	}

	@Override
	public void getZoomedCol(String[] keys, StandardForm form) {
		// TODO Auto-generated method stub
		if(form instanceof MagacinForm){
			tfSifraPreduzeca.setText(keys[0]);
			tfSifraSektora.setText(keys[1]);
			tfSifraMagacina.setText(keys[2]);
		}else if(form instanceof ArtikalForm){
			tfSifraGrupe.setText(keys[0]);
			tfSifraArtikla.setText(keys[1]);
		}else if(form instanceof PoslovnaGodinaForm){
			tfSifraPreduzeca.setText(keys[0]);
			tfPoslovnaGodina.setText(keys[1]);
		}
		
	}

	@Override
	public String[] getSelectedRowNextKeys(int index) {
		// TODO Auto-generated method stub
		return new String[]{tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim(), tfSifraMagacina.getText().trim(), 
				tfSifraGrupe.getText().trim(), tfSifraArtikla.getText().trim(), tfPoslovnaGodina.getText().trim(), "", "", "", 
				"", "", "", "", "", "", "", ""};
	}

}
